package com.hzz.filter;

import com.alibaba.fastjson.JSON;
import com.hzz.code.ResponseResult;
import com.hzz.util.Sm4Util;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ：hzz
 * @description：TODO
 * @date ：2021/6/28 9:28
 */
@Order(Ordered.LOWEST_PRECEDENCE - 1)
@Component
public class MyFilter extends OncePerRequestFilter {

    //被过滤器处理的请求
    public static final String[] dispatchPattern = {"/test/**"};

    //这边返回false代表这个请求要被过滤，true则是不被过滤
    private boolean checkFilter(String path) {
        for (String s : dispatchPattern) {
            AntPathMatcher antPathMatcher = new AntPathMatcher();
            if (antPathMatcher.match(s, path)) {
                return false;
            }
        }
        return true;
    }

    private String getRequestPath(HttpServletRequest request) {
        String url = request.getServletPath();

        if (request.getPathInfo() != null) {
            url += request.getPathInfo();
        }

        return url;
    }

    //不需要被过滤的请求
    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
        return checkFilter(getRequestPath(request));
    }

    //过滤到的请求进行的处理
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("先进入了过滤器");

        BodyReaderRequestWrapper requestWrapper = new BodyReaderRequestWrapper(httpServletRequest);

        final String bodyString = requestWrapper.getBodyString();
        System.out.println("拿到了请求的参数为:"+bodyString);

        ResponseWrapper responseWrapper=new ResponseWrapper(httpServletResponse);

        // 提取业务json数据填充到body中
        final RequestBodyServletRequestWrapper requestBodyServletRequestWrapper = new RequestBodyServletRequestWrapper(requestWrapper, bodyString);


        //这边是调用过滤器链，这里就等于是执行了请求的方法
       filterChain.doFilter(requestBodyServletRequestWrapper,responseWrapper);

        String resultdate = new String(responseWrapper.getBytes());

        String secret="st094404eea8c34b";
        final String responseEncryptData = Sm4Util.encryptEcb(secret,resultdate );
        System.out.println("加密后的返回值:"+responseEncryptData);
        ResponseResult<Object> objectResponseResult = ResponseResult.getOK(responseEncryptData);

        httpServletResponse.setContentLength(-1);
        httpServletResponse.setHeader("content-type", "application/json; charset=utf-8");
        try (PrintWriter out = httpServletResponse.getWriter()) {
            out.write(JSON.toJSONString(objectResponseResult));
        }


    }
}
